package loja.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import loja.model.venda.Venda;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

@FacesConverter(forClass = Venda.class)
public class VendaConverter implements Converter {

	private ServiceFactory serviceFactory = ServiceFactoryHelper
			.getServiceFactory();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1,
			String stringVenda) {

		if (stringVenda == null)
			return null;

		if (stringVenda.trim().length() == 0)
			return null;

		Long idVenda = Long.valueOf(stringVenda);
		return this.serviceFactory.getVendaService().consultarVendaPorId(
				idVenda);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object objVenda) {
		if (objVenda == null)
			return null;
		Venda venda = (Venda) objVenda;
		return String.valueOf(venda.getIdVenda());
	}

}
